<?php

namespace App\Http\Controllers\Work;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Service\Circle\CircleService;
use App\Service\Customer\CustomerService;
use App\Service\Work\WorksRelateService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class WorksRelateController extends Controller
{
    public function randWorks(Request $request)
    {
        $page = $request->input('page');
        $uid = $request->input('uid');

        $offest = ($page-1)*10;
        //$page = $page*10;
        $works = WorksRelateService::getRandWorks($uid,10,$offest);
        if(!CustomerService::findUser(['uid'=>$uid])){
            Result::msg('-16',"error","无有效用户信息");
        }
        if($works){
            Result::msg('0',"success",$works);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }
	
	/**
     * 举报作品
     */
    public function informWorks(Request $request)
    {
        $work_id = $request->work_id;
        $uid = $request->uid;
        $introduce = $request->message;
        Result::veryShort($introduce);
        //是否有此作品
        $works = WorksRelateService::getUserWork($work_id);
        if(!$works){
            Result::msg('-17',"error","无有效文章");
        }
        //是否已经举报
        $is_info = WorksRelateService::isUserInfo($uid, $work_id);
        if(!$is_info){
            Result::msg('-41',"error","你已经举报过此文章");
        }

        $data = array(
            'uid' => $uid,
            'article_id' => $work_id,
            'introduce' => $introduce,
            'created_at' => date("Y-m-d H:i:s")
        );
        $add_result = WorksRelateService::addInfWorks($data);

        if(!$add_result){
            Result::msg('-11',"error","更新失败");
        }

        Result::msg('0',"success",$data);
    }

    /**
     * 上架下架
     * @param Request $request
     */
    public function worksSale(Request $request)
    {
        $article_id = $request->input('articleid');
        //获取商品状态
        $article_info = WorksRelateService::getUserWork($article_id);

        if($article_info->status == 0){
            Result::msg('-34',"error","商品未审核");
        }
        $is_onsale = $article_info->is_onsale ? 0 : 1;
        $result = WorksRelateService::changeWorksSale($article_id,$is_onsale);
        if(!$result){
            Result::msg('-11',"error","更新失败");
        }

        Result::msg('0',"success",[]);
    }

    /**
     * 首页随机推荐
     */
    public function worksRandRecmd(Request $request)
    {
        //获得随机推荐数据
        $works_ids_array = Redis::srandmember('workslistids',10);
        if(!$works_ids_array){
            Result::msg('0','success',[]);
        }
        $user_info = [];
        $works_info = [];
        //首页缓存hash获取数据
        foreach($works_ids_array as $key => $works_id) {
            $works = Redis::hgetall($works_id);
            if($works){
                $works_info[] = $works;
                $user_info[] = $works['author_uid'];
            }else{
                //查询数据库 放入缓存
            }

        }

        $user_infos = CustomerService::userNameAndHeadWhereIn($user_info);
        foreach($works_info as $k => $works){
            foreach($user_infos as $user_info){
                //拼接数据
                if($user_info['uid'] == $works['author_uid']){
                    $works_info[$k]['nickname'] = $user_info['nickname'];
                    $works_info[$k]['headimgurl'] = $user_info['headimgurl'];
                }
            }
        }

        Result::msg('0','success',$works_info);
    }
}